from math import factorial
import os
import sys
from functools import reduce

"""
a1:  区间值模糊数 格式为([],[])

self.q: q值

a2:     区间值模糊数 格式为([],[])

编写时间:2022-6-30 
编写人:吴卓成
参考文献:Pythagorean fuzzy linguistic Muirhead mean
"""

os.path.join(os.path.dirname(__file__), '../../')
sys.path.append(os.path.join(os.path.dirname(__file__), '../../'))

from Utilities.AutoGetOperator.selectPackage import get_func

Operator_IVQ_ROFS=get_func(r'Operators/OperationOperators/OperatorIVQROF.py','Operator_IVQ_ROFS')

class Muirheadmean(Operator_IVQ_ROFS):
    def __init__(self, data_list=[], weight_list=[], q=3, x=2, a=2, b=2):
        super().__init__(data_list, weight_list, q, x, a, b)
        res=[1]+[0]*(len(self.data_list)-1)
        self.set_x(res)#p的集合
        self.temp_res=[0 for i in range(len(self.data_list))]#储存结果
        self.res=[]#排序相乘结果的集合
        self.st=[False for i in range(len(self.data_list))]#标志状态
    def bfs(self,u):
        """
        function:   全排列
        u       :   从0开始
        self.x  :   p的集合

        """
        if(u==len(self.data_list)):
            print(self.temp_res)
            for index  in range(len(self.temp_res)):#将每一个模糊数先转化
                self.temp_res[index]=self.pow(self.temp_res[index],self.x[index],self.q)
            res=self.temp_res[0]
            temp_res=self.temp_res[1::]
            for index,enum in enumerate(temp_res,start=1):
                res=self.multi(res,enum,self.q)
            self.res.append(res)
            return
        for i in range(len(self.data_list)):
            if(not self.st[i]):
                self.temp_res[u]=self.data_list[i]
                self.st[i]=True
                self.bfs(u+1)
                #恢复现场
                self.temp_res[u]=0
                self.st[i]=False
    def getResult(self, *waste1, **waste2):
        self.bfs(0)
        data=self.res
        res=data[0]
        data=data[1::]
        n=factorial(len(self.data_list))

        for enum in data:
            res=self.add(res,enum,self.q)
        res=self.kmulti(res,1/n,self.q)
        k=reduce(lambda x,y:x+y,self.x)
        return self.pow(res,1/k,self.q)
# 测试数据 和文章一致
# data=[([0.5,0.5],[0.3,0.3]),([0.7,0.7],[0.5,0.5]),([0.8,0.8],[0.2,0.2])]
# p=[1,0.5,0.4]
# pt=Muirheadmean(data)
# pt.set_x(p)
# pt.set_q(2)
# print(pt.data_list)
# print(pt.x)
# print(pt.getResult())
if __name__ == '__main__':
    data_list=[([0.31, 0.24], [0.73, 0.72]), ([0.97, 0.12], [0.12, 0.05]),
    ([0.8, 0.52], [0.73, 0.15]), ([0.91, 0.49], [0.42, 0.47]),([0.95, 0.06], [0.19, 0.1])]
    weight_list=[0.1, 0.2, 0.3, 0.1, 0.3],
    Pa =Muirheadmean(data_list)
    # Pa = GA(list1)
    # Pa = WA(list1,weight_list)
    # Pa = WGA(list1,weight_list)
    # Pa = OWA(list1,weight_list)
    print(Pa.getResult())      
        